// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-10-25 10:36:25
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 14:34:05
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Constraint/TEMPDParser.cpp
 */

#include "Import/BDFImport/BulkData/Constraint/TEMPDParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/ITEMPDService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (TEMPD);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/TEMPD/TEMPD.md>
*/

// 以下需要修改
enum TEMPD1Index : std::uint8_t
{
    TEMPD1Index_SID1 = 1,
    TEMPD1Index_T1   = 2,
    TEMPD1Index_SID2 = 3,
    TEMPD1Index_T2   = 4,
    TEMPD1Index_SID3 = 5,
    TEMPD1Index_T3   = 6,
    TEMPD1Index_SID4 = 7,
    TEMPD1Index_T4   = 8,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseTEMPD (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    // 定义两个 TEMPDData 类型的临时对象，用于存储解析出的单点约束数据
    SG::DataStructure::FEM::TEMPDData      tempData;
    SG::DataStructure::FEM::TEMPDDataArray tempDataArray;
    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;

    // 初始化当前字段的索引值
    int curIndex = 0;

    // 遍历分割后的词条信息
    for (auto& curStr : dividedEntryInfo)
    {
        // 检查当前字段是否为空白，如果为空则跳过
        bool const isEmpty = is_all_space (curStr);
        if (isEmpty)
        {
            continue;
            curIndex++;  // 增加索引以便继续遍历
        }

        // 根据索引值决定当前字段的含义
        switch (curIndex)
        {
        case TEMPD1Index_SID1:
            tempDataArray.push_back (tempData);
            // 解析并存储约束集合的识别号
            tempDataArray.back ().m_sId = convertToI64 (curStr);
            break;
        case TEMPD1Index_T1:
            // 解析并存储第一个节点的 ID
            tempDataArray.back ().m_t = convertToReal (curStr);
            break;
        case TEMPD1Index_SID2:
            tempDataArray.push_back (tempData);
            // 解析并存储约束集合的识别号
            tempDataArray.back ().m_sId = convertToI64 (curStr);
            break;
        case TEMPD1Index_T2:
            // 解析并存储第一个节点的 ID
            tempDataArray.back ().m_t = convertToReal (curStr);
            break;
        case TEMPD1Index_SID3:
            tempDataArray.push_back (tempData);
            // 解析并存储约束集合的识别号
            tempDataArray.back ().m_sId = convertToI64 (curStr);
            break;
        case TEMPD1Index_T3:
            // 解析并存储第一个节点的 ID
            tempDataArray.back ().m_t = convertToReal (curStr);
            break;
        case TEMPD1Index_SID4:
            tempDataArray.push_back (tempData);
            // 解析并存储约束集合的识别号
            tempDataArray.back ().m_sId = convertToI64 (curStr);
            break;
        case TEMPD1Index_T4:
            // 解析并存储第一个节点的 ID
            tempDataArray.back ().m_t = convertToReal (curStr);
            break;

        default:
            // 默认情况下不做任何处理
            break;
        }
        // 增加当前字段的索引以便处理下一个字段
        curIndex++;
    }

    // 如果存在第二个节点约束，则将其 SID 与第一个节点一致，并将其添加到数据库
    for (size_t i = 0; i < tempDataArray.size (); i++)
    {
        if (dbServiceFactory->get<ITEMPDService> ()->Add (tempDataArray[i]) == Common::Status::STATUS_Failure)
        {
            throw std::runtime_error ("Failed to add TEMPD data to database.");
            return Common::Status::STATUS_Failure;
        }
    }

    // 将第一个节点的约束数据添加到数据库
    return Common::Status::STATUS_Success;
}